package com.zhanglijie.improve.aqs.exclusive.interupt;

import java.util.concurrent.locks.Condition;

import com.zhanglijie.improve.aqs.exclusive.MyReentrantLock;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/12/4 0004 18:50
 */
public class InterruptDemo {
    private static final transient MyReentrantLock lock = new MyReentrantLock();
    private static final Condition available = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {



        // Thread t1 = new Thread(()->{
        //     lock.lock();
        //     try {
        //         try {
        //             System.out.println("t1即将阻塞");
        //             available.await();//释放锁后下面
        //         } catch (InterruptedException e) {
        //             e.printStackTrace();
        //         }
        //         System.out.println("继续指向");
        //         System.out.println("正常继续执行");
        //     } finally {
        //         System.out.println("t1 exit ");
        //         lock.unlock();
        //     }
        // },"t1");
        //
        // t1.start();
        // Thread.sleep(3000);
        // Thread t2 =new Thread(()->{
        //     lock.lock();//等t1线程执行await后 t2能抢到锁
        //     try {
        //         available.signal();//唤醒t1 如果此时唤醒的t1将它挪到同步队列末尾是取消状态就会直接unpark t1
        //         System.out.println("唤醒t1");
        //         Thread.sleep(Integer.MAX_VALUE);
        //     } catch (InterruptedException e) {
        //         e.printStackTrace();
        //     } finally {
        //         System.out.println("t2 exit");
        //         lock.unlock();
        //     }
        // },"t3");
        // t2.start();


        System.out.println("=================================================================================================");

        Thread t4 =new Thread(()->{
            lock.lock();//等t1线程执行await后 t2能抢到锁
            try {
                System.out.println("exe");
                Thread.currentThread().interrupt();
                System.out.println("continue exe");
            }  finally {
                System.out.println("t4 exit");
                lock.unlock();
            }
        },"t4");
        t4.start();
    }
}
